package cn.bopomo.dealer.base.chart;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.jfree.data.general.DefaultPieDataset;

import cn.bopomo.dealer.base.action.BaseAction;

public class PieChartGenAction extends BaseAction {

	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-control","no-cache");
		
		
		String datasetAddr = request.getParameter("dataset");
		if (datasetAddr == null || "".equals(datasetAddr)){
			response.getWriter().write("");
			return null;
		}
		
		//获取数据源
		String[] d = datasetAddr.split("\\|\\|");
		DefaultPieDataset dataset = (DefaultPieDataset)request.getSession().getAttribute(d[0]);
		String title = d[1] + "数据统计图";
		if (dataset == null){
			response.getWriter().write("");
			return null;
		}
		
		String fileName = ChartUtil.generatePieChart(dataset,title,400,350,request.getSession(),new PrintWriter(response.getOutputStream()));
		response.getWriter().write("<fileName>" + fileName + "</fileName>");
		
		return null;
	}

}
